package com.users.rn.rncommon.plugin;

import android.text.TextUtils;

import com.users.rn.common.init.ALHReactNativeInit;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.Serializable;

/**
 * @Description:
 * @Author: bluce
 * @Date: 2020/12/3 18:52
 */
public class PluginInformation implements Serializable {
    private static String basicDir = null;

    public static String getPluginPath(String pluginId, Long pluginVersion) {
        return basicDir + File.separator + pluginId + File.separator + pluginVersion + File.separator;
    }

    public static void setBasicDir(String basicDir) {
        PluginInformation.basicDir = basicDir;
    }

    public static String getBasicDir() {
        return basicDir;
    }

    //动态库唯一标志
    private String pluginName;
    //最小主客版本
    private long pluginVersion;
    //最小主客版本
    private long miniAppVersion;
    //最大主客版本
    private long maxAppVersion;
    //插件下载地址
    private String url;
    //插件md5值
    private String md5;

    //是否拆包、默认拆包;true 不拆包，false拆包
    private boolean unpack = false;

    public String getPluginName() {
        return pluginName;
    }

    public void setPluginName(String pluginName) {
        this.pluginName = pluginName;
    }

    public long getPluginVersion() {
        return pluginVersion;
    }

    public void setPluginVersion(long pluginVersion) {
        this.pluginVersion = pluginVersion;
    }

    public long getMiniAppVersion() {
        return miniAppVersion;
    }

    public void setMiniAppVersion(long miniAppVersion) {
        this.miniAppVersion = miniAppVersion;
    }

    public long getMaxAppVersion() {
        return maxAppVersion;
    }

    public void setMaxAppVersion(long maxAppVersion) {
        this.maxAppVersion = maxAppVersion;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getMd5() {
        return md5;
    }

    public void setMd5(String md5) {
        this.md5 = md5;
    }

    public boolean isUnpack() {
        return unpack;
    }

    public void setUnpack(boolean unpack) {
        this.unpack = unpack;
    }

    public String getFilePath() {
        return basicDir + File.separator + pluginName + File.separator + pluginVersion + File.separator;
    }

    private String getFileName() {
        return "packets" + url.substring(url.lastIndexOf("/"));
    }

    public String getFullFilePath() {
        return getFilePath() + getFileName();
    }

    public static PluginInformation deserialize(String json) throws JSONException {
        PluginInformation pluginInformation = null;
        if (!TextUtils.isEmpty(json)) {
            JSONObject jsonObject = new JSONObject(json);
            if (json != null && jsonObject != JSONObject.NULL && jsonObject.length() > 0) {
                pluginInformation = new PluginInformation();

                if (!jsonObject.isNull("pluginName")) {
                    pluginInformation.pluginName = jsonObject.getString("pluginName");
                }

                if (!jsonObject.isNull("pluginVersion")) {
                    pluginInformation.pluginVersion = jsonObject.getLong("pluginVersion");
                }

                if (!jsonObject.isNull("miniAppVersion")) {
                    pluginInformation.miniAppVersion = jsonObject.getLong("miniAppVersion");
                }

                if (!jsonObject.isNull("maxAppVersion")) {
                    pluginInformation.maxAppVersion = jsonObject.getLong("maxAppVersion");
                }

                if (!jsonObject.isNull("url")) {
                    pluginInformation.url = jsonObject.getString("url");
                }

                if (!jsonObject.isNull("md5")) {
                    pluginInformation.md5 = jsonObject.getString("md5");
                }
                if (!jsonObject.isNull("unpack")) {
                    pluginInformation.unpack = jsonObject.optBoolean("unpack", true);
                }
            }
        }

        return pluginInformation;
    }

    @Override
    public String toString() {
        return "PluginInformation{" +
                "pluginName='" + pluginName + '\'' +
                ", pluginVersion='" + pluginVersion + '\'' +
                ", miniAppVersion='" + miniAppVersion + '\'' +
                ", maxAppVersion=" + maxAppVersion +
                ", url='" + url + '\'' +
                ", md5=" + md5 +
                ", unpack='" + unpack + '\'' +
                '}';
    }


}
